Skip to content

Add page engagement tracking with scroll depth and dwell time - #13

Open
winsmith wants to merge 5 commits into
mainfrom
claude/fervent-fermi-ftauy2
Open

winsmith wants to merge 5 commits into
mainfrom
claude/fervent-fermi-ftauy2

Conversation

@winsmith

Copy link
Copy Markdown
Contributor

Summary

This PR adds page engagement tracking to the TelemetryDeck Web SDK, enabling measurement of user interaction with pages through scroll depth and time spent viewing.

Key Changes

  • New pageLeave signal: Sends a TelemetryDeck.Web.pageLeave event when a page is hidden or unloaded, capturing:

    • scrollDepth: Deepest point of the page viewed (0-100%)
    • scrollDepthMilestone: Highest milestone reached (0, 25, 50, 75, or 100)
    • engagedSeconds: Time the page was visible in the foreground
  • Refactored signal sending: Extracted common signal building logic into send.mjs with:

    • buildBody(): Constructs signal payload with app ID, URL, referrer, locale, and version
    • send(): Standard fetch-based signal sending
    • sendBeacon(): Uses navigator.sendBeacon() for reliable delivery during page unload
  • New configuration module (config.mjs): Centralizes script attribute parsing and initialization logic, including:

    • App ID and API endpoint configuration
    • Test mode detection (localhost, 127.0.0.1, file:// URLs)
    • Page engagement opt-out via data-page-engagement="false"
  • Page engagement tracking (page-engagement.mjs): Implements scroll depth measurement and dwell time tracking with:

    • Coalesced scroll/resize event handling via requestAnimationFrame
    • Visibility state tracking to measure only foreground time
    • Single-send guarantee using visibilitychange and pagehide events
  • Updated tests: Comprehensive test coverage for:

    • Page engagement signal structure and values
    • Scroll depth calculation for various page heights
    • Opt-out functionality
    • Referrer tracking across navigations
  • Documentation: Added README section explaining the new signals, parameters, and example analytics query

Implementation Details

  • Page engagement tracking is enabled by default and can be disabled with data-page-engagement="false"
  • Scroll depth is measured as the percentage of page height that has been in the viewport
  • Engaged time only counts when the page is visible (tab in foreground)
  • The pageLeave signal uses sendBeacon() to ensure delivery even during page unload
  • Both visibilitychange and pagehide events are handled, but the signal is sent only once per page load

https://claude.ai/code/session_01BjanDUedGAKE6LDgN8Eo5h

@winsmith
winsmith force-pushed the claude/fervent-fermi-ftauy2 branch 2 times, most recently from 209c906 to cc8b77e Compare September 22, 2026 13:00
claude and others added 4 commits September 22, 2026 15:16
Send a second signal per page load, `TelemetryDeck.Web.pageLeave`, the
first time the page is hidden or unloaded. It carries the maximum scroll
depth reached (percent), a 25/50/75/100 milestone bucket and the number
of seconds the page was visible, as `TelemetryDeck.PageEngagement.*`
parameters. The signal shares `url` and `referrer` with the pageview.

The signal is sent with `navigator.sendBeacon` as a plain-text body
(no CORS preflight, so it survives unload), falling back to a keepalive
fetch. Tracking is on by default and can be disabled with
`data-page-engagement="false"`.

To make room for this, the entry module is split into `config.mjs`
(script-tag attributes, test-mode detection), `send.mjs` (body builder,
fetch and beacon transports) and `page-engagement.mjs` (measurement).
The pageview request itself is unchanged.

Tests cover the new signal, the short-page case, the opt-out and that
only one signal is sent even though both visibilitychange and pagehide
fire. Also fixes the referrer fixture, which loaded a non-existent
unminified bundle, and the duplicated empty-app-id test.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BjanDUedGAKE6LDgN8Eo5h
All three workflows failed before running a command: setup-node@v2's
pnpm cache hit the retired cache API, upload-artifact@v3 is refused by
GitHub, and pnpm 6.19 cannot read the pnpm 8 lockfile in this repo.
Bump checkout, setup-node and upload-artifact to v4, use pnpm 8 and
Node 20 in ci.yml, deploy.yml and release.yml.

Also add a test that loads the SDK with `async`, which the README now
recommends, and format the README snippets.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BjanDUedGAKE6LDgN8Eo5h
WebKit measured 49% where Chromium and Firefox measured 50% for a
scroll of exactly one viewport on a four-viewport page, because of
sub-pixel layout differences and the heading's default margin. Scroll
1.2 viewports instead (55%) and zero the fixture margins so the
milestone assertion no longer sits on a rounding edge.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BjanDUedGAKE6LDgN8Eo5h
@winsmith
winsmith force-pushed the claude/fervent-fermi-ftauy2 branch from cc8b77e to b3dfcbf Compare September 22, 2026 13:17
The generated stub left package-ecosystem empty, which Dependabot rejects.
Watch the npm ecosystem, which also covers the pnpm lockfile, and the
GitHub Actions used in the workflows.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants